﻿using Padsurface.CoreAudio.Common;
using Padsurface.CoreAudio.Components.MMDevice.Interfaces;
using System;
using System.Runtime.InteropServices;

namespace Padsurface.CoreAudio.Components.MMDevice
{
    public class ActivateAudioInterfaceCompletionHandler : IActivateAudioInterfaceCompletionHandler, IAgileObject
    {
        public event EventHandler<InterfaceActivatedEventArgs> InterfaceActivated;
        
        public void ActivateCompleted(IActivateAudioInterfaceAsyncOperation activateOperation)
        {
            try
            {
                if (activateOperation == null) throw new ArgumentNullException("activateOperation");

                int activateResult;
                object activatedInterface;
                Marshal.ThrowExceptionForHR(activateOperation.GetActivateResult(out activateResult, out activatedInterface));

                if (InterfaceActivated != null)
                    InterfaceActivated(this, new InterfaceActivatedEventArgs { ActivatedInterface = activatedInterface });
            }
            catch (Exception ex)
            {
                if (InterfaceActivated != null)
                    InterfaceActivated(this, new InterfaceActivatedEventArgs { Exception = ex });
            }
        }
    }
}